package com.winit.cloudlink.rabbitmq.httpclient;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class BasicAuthHttpClientProvider implements HttpClientProvider {
    public static final int CONNECT_TIMEOUT = 10 * 1000;
    public static final int READ_TIMEOUT = 10 * 1000;
    private String username;
    private String password;

    public BasicAuthHttpClientProvider(String username, String password) {

        this.username = username;
        this.password = password;
    }

    @Override
    public boolean useSsl() {

        return false;
    }

    @Override
    public Client getClient() {

        ClientConfig clientConf = new DefaultClientConfig();
        clientConf.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, CONNECT_TIMEOUT);
        clientConf.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, READ_TIMEOUT);
        clientConf.getClasses().add(GsonMessageBodyHandler.class);

        Client client = Client.create(clientConf);
        client.setConnectTimeout(CONNECT_TIMEOUT);
        client.setReadTimeout(READ_TIMEOUT);
        client.addFilter(new HTTPBasicAuthFilter(this.username, this.password));

        return client;
    }
}